﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlueCrest.Prism.Common.UI.Ribbon;
using BlueCrest.Prism.Common.UI.Ribbon.Entities;
using Microsoft.Practices.Prism.Regions;

namespace BlueCrest.Prism.Common.UI.Services
{
    public interface IRibbonService
    {
        /// <summary>
        /// Adds Tab Entity
        /// </summary>
        /// <param name="ribbonTab"></param>
        /// <param name="id"></param>
        void AddTab(RibbonTabEntity ribbonTab, string id = null);

        void AddTab(RibbonEstate ribbonEstate, string id = null);
    }

    public class RibbonService : IRibbonService
    {
        private readonly IRegionManager _regionManager;
        readonly IRegion _ribbonRegion;

        public RibbonService(IRegionManager regionManager)
        {
            _regionManager = regionManager;
            _ribbonRegion = _regionManager.Regions[RegionNames.RibbonRegion];
        }

        /// <summary>
        /// Adds Tab Entity
        /// </summary>
        /// <param name="ribbonTab"></param>
        /// <param name="id"></param>
        public void AddTab(RibbonTabEntity ribbonTab, string id = null)
        {
            var ribbonEstate = new RibbonEstate
                                   {
                                       Tabs = new List<RibbonTabEntity>{ribbonTab}
                                   };

            AddTab(ribbonEstate, id);
        }

        public void AddTab(RibbonEstate ribbonEstate, string id = null)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = Guid.NewGuid().ToString();
            }

            //TODO: At the moment it is adding tab using GUID, we need to able to remove when needed (i.e. when modules unload)
            _ribbonRegion.Add(ribbonEstate, id);
        }
    }
}
